from django.contrib import admin

from celery_tasks.static_html.tasks import generate_static_list_search_html,generate_static_sku_detail_html
from goods import models
# Register your models here.

class GoodModelAdmin(admin.ModelAdmin):
    list_display = ('name', 'sales', 'comments', 'brand')


    def save_model(self, request, obj, form, change):
        '''当运营人员点击保存按钮时被触发'''
        generate_static_list_search_html.delay()


class SKUModelAdmin(admin.ModelAdmin):

    def save_model(self, request, obj, form, change):
        '''当运营人员点击保存按钮时被触发'''
        obj.save()
        generate_static_sku_detail_html.delay(obj.id)


    def delete_model(self, request, obj):
        '''当运营人员点击删除按钮时被触发'''
        obj.delete()
        generate_static_list_search_html.delay(obj.id)


class SKUImageAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.save()

        generate_static_sku_detail_html.delay(obj.sku.id)

        # 设置SKU默认图片
        sku = obj.sku
        if not sku.default_image_url:
            sku.default_image_url = obj.image.url
            sku.save()

    def delete_model(self, request, obj):
        sku_id = obj.sku.id
        obj.delete()

        generate_static_sku_detail_html.delay(sku_id)



admin.site.register(models.Goods, GoodModelAdmin)       #添加GoodModelAdmin将模型类与后台admin进行绑定
admin.site.register(models.SKU, SKUModelAdmin)
admin.site.register(models.SKUImage, SKUModelAdmin)